//
//  MainAreaCitysViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/20.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MainAreaCitysViewModel.h"
#import "MainTypesListTableViewCell.h"
#import "InputCommentViewModel.h"
#import "MainTypesListHeaderView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "MainTypesListTopTableViewCell.h"
#import "MainTypesListNextSteelMillsModel.h"
#import "MainAreaModel.h"
#import "MainArealCitysModel.h"

@interface MainAreaCitysViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    NSMutableArray *rightSelecShowArr;   // 选中的cell显示选中:图片
}
@property (nonatomic, strong) NSMutableArray *selectDataArr;                       // 选中的cell在顶部显示数据
@property (nonatomic, strong) NSMutableArray *selectIndexDataArr;                  // 选中的cell在顶部显示数据model:indexPath
@property (nonatomic, strong) MainArealCitysModel *model;

@end

@implementation MainAreaCitysViewModel


- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        selfTableView = tableView;
        selfTableView.delegate = self;
        selfTableView.dataSource = self;
        selfTableView.backgroundColor = [UIColor whiteColor];
        selfTableView.mj_footer.hidden = YES;
        selfTableView.mj_header.hidden = YES;
        selfTableView.estimatedRowHeight = 40;
        [selfTableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [selfTableView registerClass:[MainTypesListHeaderView class] forHeaderFooterViewReuseIdentifier:@"MainTypesListHeaderView"];
        [superView addSubview:selfTableView];
        self.model = [MainArealCitysModel new];

    }
    return self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MainTypesListHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MainTypesListHeaderView"];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footer;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.model.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTypesListTableViewCell *cell = [MainTypesListTableViewCell cellWithTableView:tableView];
    cell.showPush = NO;
    if (rightSelecShowArr.count > 0) {
        cell.hidenRighImgV = [rightSelecShowArr[indexPath.row] boolValue];        
    }
    if (indexPath.section == 0) {
        MainTypesListTopTableViewCell *cell = [MainTypesListTopTableViewCell cellWithTableView:tableView];
        cell.arr = self.selectDataArr;
        return cell;
    }
    if (_model) {
        MainArealCitysListModel *listModel = _model.data[indexPath.row];
        cell.leftStr = listModel.cityName;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) return;
    NSString *str = rightSelecShowArr[indexPath.row];
//    MainArealCitysListModel *listModel = _model.data[indexPath.row];
    if ([str isEqualToString:@"0"]) { // 显示选中
        rightSelecShowArr[indexPath.row] = @"1";
//        [self.selectDataArr removeObject:listModel.cityName];
        [self.selectIndexDataArr removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    } else {
        rightSelecShowArr[indexPath.row] = @"0";
//        [self.selectDataArr addObject:listModel.cityName];
        [self.selectIndexDataArr addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    [self keepSelet];
//    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [selfTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

// 处理保存数据
- (void)keepSelet
{
    int select = [self.dic[@"select"] intValue];
    NSMutableArray *arr = self.dic[@"arr"];
    NSMutableDictionary *lDiDic = arr[select];
    lDiDic[@"three"] = [NSMutableArray arrayWithArray:self.selectIndexDataArr];
    NSMutableArray *liArr = [NSMutableArray array];
    for (int i = 0; i < self.selectIndexDataArr.count; i ++) {
        NSInteger cout = [self.selectIndexDataArr[i] integerValue];
        MainArealCitysListModel *liModel = self.model.data[cout];
        [liArr addObject:liModel];
    }
    lDiDic[@"four"] = [NSMutableArray arrayWithArray:liArr];
    arr[select] = lDiDic;
    [self refreshSelectData];
}

- (void)refreshSelectData
{
    // 刷新最上面显示的数据
    NSArray *liArr = _dic[@"arr"];
    if (liArr.count <= 0) return;
    NSMutableArray *seleArr = [NSMutableArray array];
    for (int i = 0; i <  liArr.count; i ++) {
        NSDictionary *liDic = liArr[i];
        MainAreaListModel *arealListModel = liDic[@"two"];
        NSArray *citysArr = liDic[@"four"];
        if (citysArr.count > 0) {
            NSString *seleAppendStr;
            for (int i = 0; i < citysArr.count; i ++) {
                MainArealCitysListModel *citysListModel = citysArr[i];
                seleAppendStr = [NSString stringWithFormat:@"%@%@",arealListModel.provinceName,citysListModel.cityName];
                [seleArr addObject:seleAppendStr];
            }
        }
    }
    self.selectDataArr = seleArr;
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [selfTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSMutableArray *)selectDataArr
{
    if (!_selectDataArr) {
        _selectDataArr = [NSMutableArray array];
    }
    return _selectDataArr;
}

- (NSMutableArray *)selectIndexDataArr
{
    if (!_selectIndexDataArr) {
        _selectIndexDataArr = [NSMutableArray array];
    }
    return _selectIndexDataArr;
}


- (void)setDic:(NSMutableDictionary *)dic
{
    _dic = dic;
    [self requestMainTypeNextDetailList];
}

#pragma mark - 请求接口
- (void)requestMainTypeNextDetailList
{
    int select = [self.dic[@"select"] intValue];
    NSArray *arr = self.dic[@"arr"];
    NSDictionary *liDic = arr[select];
    MainAreaListModel *listModel = liDic[@"two"];
    NSDictionary *params = @{@"provinceId" : koString(listModel.provinceId)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kAreaCitys] lodingView:nil lodingTextString:@"" isLoding:YES parameters:params success:^(id  _Nullable response) {

        self.model = [MainArealCitysModel yy_modelWithJSON:response];
        self->rightSelecShowArr = [NSMutableArray array];
        for (int i = 0; i < self.model.data.count; i ++) {
            [self->rightSelecShowArr addObject:@"1"];
        }
        if (self.model.code == 0) {
            [self makeLastPageSelectData];
        }
    } failure:^(NSError * _Nullable error) {
        DLog(@"分类=%@",error);
    }];
}

- (void)makeLastPageSelectData
{
    int select = [_dic[@"select"] intValue];
    NSArray *arr = _dic[@"arr"];
    NSDictionary *liDic = arr[select];
    NSArray *liArr = liDic[@"three"];
    if (liArr.count > 0) { // 刷新一下选中的cell
        for (int i = 0; i < liArr.count; i ++) {
            NSInteger indexRow = [liArr[i] integerValue];
            rightSelecShowArr[indexRow] = @"0";
        }
        _selectIndexDataArr = [NSMutableArray arrayWithArray:liArr];
    }
    [selfTableView reloadData];
    [self refreshSelectData];
}

@end
